import cv2
import numpy as np

# 按名获取特征检测器
def get_detector(detector_name):
    if detector_name == 'SIFT':
        return cv2.xfeatures2d.SIFT_create()
    elif detector_name == 'SURF':
        return cv2.xfeatures2d.SURF_create()
    elif detector_name == 'ORB':
        return cv2.ORB_create()
    elif detector_name == 'BRISK':
        return cv2.BRISK_create()

img = cv2.imread('chessboard.jpg', cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 检测特征点, 并计算特征描述子
detector_name = 'SIFT'
detector = get_detector(detector_name)
kp, des = detector.detectAndCompute(gray, None)
dst = np.zeros(img.shape, np.uint8)
cv2.drawKeypoints(img, kp, dst, color=(0, 0, 255),
                  flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow(detector_name+' Corner', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
